clear all 
set more off 
set maxvar 15000 
clear matrix


*-----------------------------------------------*
* DIFFERENCE B/W 1910S AND 1940S COHORTS (IGE) 
*-----------------------------------------------*

	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if baseline_sample==1
    keep if decade==1910 | decade==1940     
    
    gen cohort_late = decade==1940
    gen interaction = cohort_late * log_father_baseline
    
**********************************************

    * Regressions by group; store results 
    foreach x in wm white wbm all {
    
    if "`x'"=="wm" local cond "if race==1 & sex==1 "
    if "`x'"=="white" local cond "if race==1 "
    if "`x'"=="wbm" local cond "if race==1 | (race==2 & sex==1)"
    if "`x'"=="all" local cond " "
    
        reg log_son_baseline log_father_baseline cohort_late interaction `cond'  [aw=wgt_sex_race], robust
        
            local temp =_b[interaction]
            local ige_`x': display %-09.3fc `temp'
            local temp =_se[interaction]
            local se_`x': display %-04.3fc `temp'
    }
    
    * Note: no ",robust"        
    reg log_son_baseline log_father_baseline cohort_late interaction if race==1 & sex==1  [aw=wgt_sex_race]
        est store wm
    reg log_son_baseline log_father_baseline cohort_late interaction if race==1  [aw=wgt_sex_race] 
        est store white
    reg log_son_baseline log_father_baseline cohort_late interaction if race==1 | (race==2 & sex==1)  [aw=wgt_sex_race]
        est store wbm
    reg log_son_baseline log_father_baseline cohort_late interaction [aw=wgt_sex_race]
        est store all

    suest wm all 
    
    test [wm_mean]interaction=[all_mean]interaction
    
        local temp =`r(p)'
        local diff_wm_all: display %-09.3fc `temp'
        display `diff_wm_all'
        
    suest white all 
    
    test [white_mean]interaction=[all_mean]interaction
    
        local temp =`r(p)'
        local diff_white_all: display %-09.3fc `temp'
        display `diff_white_all'
        
    suest wbm all 
    
    test [wbm_mean]interaction=[all_mean]interaction
    
        local temp =`r(p)'
        local diff_wbm_all: display %-09.3fc `temp'
        display `diff_wbm_all'
        
        
**-----------------------------------------------------------------------------**
**-----------------------------------------------------------------------------**
    
*-------------------------------------------------------*
* DIFFERENCE B/W 1910S AND 1940S COHORTS (RANK-RANK)
*-------------------------------------------------------*

    estimates clear

	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if baseline_sample==1
    keep if decade==1910 | decade==1940        
    
    gen cohort_late = decade==1940
    gen interaction = cohort_late * rank_father_baseline 

**********************************************
   
* Regressions by group; store results 

    foreach x in wm white wbm all {
    
    if "`x'"=="wm" local cond "if race==1 & sex==1 "
    if "`x'"=="white" local cond "if race==1 "
    if "`x'"=="wbm" local cond "if race==1 | (race==2 & sex==1)"
    if "`x'"=="all" local cond " "
    
        reg rank_son_baseline rank_father_baseline cohort_late interaction `cond'  [aw=wgt_sex_race], robust
    
        local temp =_b[interaction]
        local rank_`x': display %-09.3fc `temp'
        local temp =_se[interaction]
        local se_rank_`x': display %-04.3fc `temp'
        
    }
            

    reg rank_son_baseline rank_father_baseline cohort_late interaction if race==1 & sex==1  [aw=wgt_sex_race]
    est store wm

    reg rank_son_baseline rank_father_baseline cohort_late interaction if race==1  [aw=wgt_sex_race]
    est store white
    
    reg rank_son_baseline rank_father_baseline cohort_late interaction if race==1 | (race== 2 & sex==1)  [aw=wgt_sex_race]
    est store wbm

    reg rank_son_baseline rank_father_baseline cohort_late interaction [aw=wgt_sex_race]
    est store all
    
    suest wm all 
    
    test [wm_mean]interaction=[all_mean]interaction
    
        local temp =`r(p)'
        local diff_wm_all_rank: display %-09.3fc `temp'
        display `diff_wm_all_rank'
        
    suest white all 
    
    test [white_mean]interaction=[all_mean]interaction
    
        local temp =`r(p)'
        local diff_white_all_rank: display %-09.3fc `temp'
        display `diff_white_all_rank'
            
    suest wbm all 
    
    test [wbm_mean]interaction=[all_mean]interaction
    
        local temp =`r(p)'
        local diff_wbm_all_rank: display %-09.3fc `temp'
        display `diff_wbm_all_rank'

*************************
* Start writing table
*************************
    file open Table1 using "$Mydirectory2/appendix_a/Table_sureg_decline.tex", write replace


    file write Table1 "\begin{tabularx}{\hsize}{@{\hskip\tabcolsep\extracolsep\fill} l c c}" _n
    file write Table1 "\toprule" _n
    file write Table1 "\addlinespace[2ex]" _n   
    file write Table1 "& IGE & Rank-rank \\" _n
    file write Table1 "\addlinespace[1ex]" _n
    file write Table1 "\midrule" _n
    
    file write Table1 "\addlinespace[1.5ex]" _n
    file write Table1 "\quad \textit{Difference between 1910s and 1940s cohorts:} &  &  \\ "
    file write Table1 "\addlinespace[1.5ex]" _n

    file write Table1 "\quad \quad White men & `ige_wm' & `rank_wm' \\ "
    file write Table1 "  & (`se_wm') & (`se_rank_wm') \\ "
    
    file write Table1 "\addlinespace[1ex]" _n
    
    file write Table1 "\quad \quad All whites & `ige_white' & `rank_white' \\ "
    file write Table1 "  & (`se_white') & (`se_rank_white') \\ "
    
    file write Table1 "\addlinespace[1ex]" _n
    
    file write Table1 "\quad \quad All whites \& Black men & `ige_wbm' & `rank_wbm' \\ "
    file write Table1 "  & (`se_wbm') & (`se_rank_wbm') \\ "
    
    file write Table1 "\addlinespace[1ex]" _n
    
    file write Table1 "\quad \quad Representative sample & `ige_all' & `rank_all' \\ "
    file write Table1 "  & (`se_all') & (`se_rank_all') \\ "
    
    file write Table1 "\addlinespace[1.5ex]" _n 
    file write Table1 "\quad P-value (white men vs. rep. sample) & `diff_wm_all' & `diff_wm_all_rank' \\"
    file write Table1 "\addlinespace[0.5ex]" _n 
    file write Table1 "\quad P-value (all whites vs. rep. sample) & `diff_white_all' & `diff_white_all_rank' \\"
    file write Table1 "\addlinespace[0.5ex]" _n
    file write Table1 "\quad P-value (all whites \& Black men vs. rep. sample) & `diff_wbm_all' & `diff_wbm_all_rank' \\"
    file write Table1 "\addlinespace[1ex]" _n
    file write Table1 "\midrule" _n

**-----------------------------------------------------------------------------**
**-----------------------------------------------------------------------------**

*-------------------------------------------------*
* LINEAR DECLINE USING 1910-1940S COHORTS (IGE)
*-------------------------------------------------*

    estimates clear 

	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if decade>=1910 & decade<=1940 
    keep if baseline_sample==1

    * Generate interaction term for decline
    sort dob
    egen time = group(dob)
    replace time = time-1   
    
    gen interaction = log_father_baseline*time 
    
    estimates clear
    
    foreach x in wm white wbm all {
    
    if "`x'"=="wm" local cond "if race==1 & sex==1 "
    if "`x'"=="white" local cond "if race==1 "
    if "`x'"=="wbm" local cond "if race==1 | (race==2 & sex==1)"
    if "`x'"=="all" local cond " "
    
        reg log_son_baseline log_father_baseline interaction i.time `cond'  [aw=wgt_sex_race], robust
    
        local temp =_b[interaction]
        local ige_`x': display %-09.4fc `temp'
        local temp =_se[interaction]
        local se_`x': display %-05.4fc `temp'
        
    }

    quietly reg log_son_baseline log_father_baseline interaction i.time if race==1 & sex==1  [aw=wgt_sex_race]
        est store wm
    
    quietly reg log_son_baseline log_father_baseline interaction i.time if race==1  [aw=wgt_sex_race]
        est store white
        
    quietly reg log_son_baseline log_father_baseline interaction i.time if  race==1 | (race==2 & sex==1)  [aw=wgt_sex_race]
        est store wbm
        
    quietly reg log_son_baseline log_father_baseline interaction i.time [aw=wgt_sex_race]
        est store all

    
    suest wm all
    
    test [wm_mean]interaction=[all_mean]interaction
    
        local temp =`r(p)'
        local diff_wm: display %-09.3fc `temp'
        display `diff_wm'
        
    suest white all
        
    test [white_mean]interaction=[all_mean]interaction
    
        local temp =`r(p)'
        local diff_white: display %-09.3fc `temp'
        display `diff_white'
        
    suest wbm all
    
    test [wbm_mean]interaction=[all_mean]interaction
    
        local temp =`r(p)'
        local diff_wbm: display %-09.3fc `temp'
        display `diff_wbm'

**-----------------------------------------------------------------------------**
**-----------------------------------------------------------------------------**       
   
*-------------------------------------------------*
* LINEAR DECLINE USING 1910-1940S COHORTS (RANKS)
*-------------------------------------------------*

	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if decade>=1910 & decade<=1940 
    keep if baseline_sample==1

    * Generate interaction term for decline
    sort dob
    egen time = group(dob)
    replace time = time-1   
        
    estimates clear
    gen interaction = rank_father_baseline*time
    
    foreach x in wm white wbm all {
    
    if "`x'"=="wm" local cond "if race==1 & sex==1 "
    if "`x'"=="white" local cond "if race==1 "
    if "`x'"=="wbm" local cond "if race==1 | (race==2 & sex==1)"
    if "`x'"=="all" local cond " "
    
        reg rank_son_baseline rank_father_baseline interaction i.time `cond'  [aw=wgt_sex_race], robust
    
        local temp =_b[interaction]
        local rank_`x': display %-09.4fc `temp'
        local temp =_se[interaction]
        local se_rank_`x': display %-05.4fc `temp'
        
    }

    quietly reg rank_son_baseline rank_father_baseline interaction i.time if race==1 & sex==1  [aw=wgt_sex_race]
        est store wm
        
    quietly reg rank_son_baseline rank_father_baseline interaction i.time if race==1   [aw=wgt_sex_race]
        est store white

    quietly reg rank_son_baseline rank_father_baseline interaction i.time if race==1 | (race==2 & sex==1)  [aw=wgt_sex_race]
        est store wbm
    
    quietly reg rank_son_baseline rank_father_baseline interaction i.time [aw=wgt_sex_race]
        est store all
    
    suest wm all
    
    test [wm_mean]interaction=[all_mean]interaction
    
        local temp =`r(p)'
        local diff_wm_rank: display %-09.3fc `temp'
        display `diff_wm_rank'
        
    suest white all
        
    test [white_mean]interaction=[all_mean]interaction
    
        local temp =`r(p)'
        local diff_white_rank: display %-09.5fc `temp'
        display `diff_white_rank'
        
    suest wbm all
    
    test [wbm_mean]interaction=[all_mean]interaction
    
        local temp =`r(p)'
        local diff_wbm_rank: display %-09.3fc `temp'
        display `diff_wbm_rank'

*************************
* Finish writing table
*************************

    file write Table1 "\addlinespace[1.5ex]" _n
    file write Table1 "\quad \textit{Linear decline using 1910s--1940s cohorts:} &  &  \\ "
    file write Table1 "\addlinespace[1.5ex]" _n

    file write Table1 "\quad \quad White men & `ige_wm' & `rank_wm' \\ "
    file write Table1 "  & (`se_wm') & (`se_rank_wm') \\ "
    
    file write Table1 "\addlinespace[1ex]" _n
    
    file write Table1 "\quad \quad All whites & `ige_white' & `rank_white' \\ "
    file write Table1 "  & (`se_white') & (`se_rank_white') \\ "
    
    file write Table1 "\addlinespace[1ex]" _n
    
    file write Table1 "\quad \quad All whites \& Black men & `ige_wbm' & `rank_wbm' \\ "
    file write Table1 "  & (`se_wbm') & (`se_rank_wbm') \\ "
    
    file write Table1 "\addlinespace[1ex]" _n
    
    file write Table1 "\quad \quad Representative sample & `ige_all' & `rank_all' \\ "
    file write Table1 "  & (`se_all') & (`se_rank_all') \\ "
    
    file write Table1 "\addlinespace[1.5ex]" _n 
    file write Table1 "\quad P-value (white men vs. rep. sample) & `diff_wm' & `diff_wm_rank' \\"
    file write Table1 "\addlinespace[0.5ex]" _n 
    file write Table1 "\quad P-value (all whites vs. rep. sample) & `diff_white' & `diff_white_rank' \\"
    file write Table1 "\addlinespace[0.5ex]" _n
    file write Table1 "\quad P-value (all whites \& Black men vs. rep. sample) & `diff_wbm' & `diff_wbm_rank' \\"   
    file write Table1 "\addlinespace[1ex]" _n
    file write Table1 "\bottomrule" _n
    
    file write Table1 "\end{tabularx}" _n
    file close Table1